
package com.netease.xone.fragment;

import protocol.XoneCallBack;
import protocol.XoneService;
import Utils.StackTraceUtil;
import Utils.XoneUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.xone.R;
import com.netease.xone.app.XoneApp;
import com.netease.xone.widget.CustomDialog.OnCustomDlgCancelListener;

import db.manager.ManagerAccount;
import db.manager.ManagerAccount.Account;

public class FragFeedBack extends FragmentBase {

    private static final String TAG = "FragFeedBack";

    private EditText mMessageEdit;// 反馈文本编辑框
    private EditText mEmailEdit;// 反馈邮箱编辑框
    private static String DEFAULT_CONTENT = "日志";
    private String mMessage;
    private String mEmail;
    private int mTrasactionId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.setting_item_feedback);
        View view = inflater.inflate(R.layout.fragment_feedback, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        mMessageEdit = (EditText)view.findViewById(R.id.edit_feedbackmessage);
        mEmailEdit = (EditText)view.findViewById(R.id.feedback_email);
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        
        com.actionbarsherlock.view.MenuItem item = menu.add(Menu.NONE, MENU_ID.menu_feedback_send, 0,getString(R.string.done));
        item.setIcon(R.drawable.icon_action_done_selector);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM );
        
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        switch (item.getItemId()) {
            case MENU_ID.menu_feedback_send:
              mMessage = mMessageEdit.getText().toString();
              mEmail = mEmailEdit.getText().toString();
              // 存储反馈邮箱
              // AppSettings.setFeedBackEmail(FeedBackActivity.this, mEmail);
              if (!TextUtils.isEmpty(mMessage.trim())) {
                  showCustomDialog(getActivity(), getString(R.string.feedbackpage_ing), mOnCustomDlgCancelListener);
//                  showWatting(getString(R.string.feedbackpage_ing));
                  XoneService.getInstance().addListener(mXoneCallBack);
                  mTrasactionId = XoneService.getInstance().doPostLog();
              } else {
                  showToast(R.string.feedbackpage_no_feedback_message);

              }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    private OnCustomDlgCancelListener mOnCustomDlgCancelListener = new OnCustomDlgCancelListener(){

        @Override
        public void onCancel() {
            XoneService.getInstance().cancelTransaction(mTrasactionId);
        }  
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mXoneCallBack);
    }
    @Override
    public boolean needShowMenu() {
        return false;
    }
    private void doFeedBack(String fileId) {
         String userid = null;
         Account acc = ManagerAccount.getInstance().getCurrentAccount();
         if(acc != null){
             userid = acc.mNickName;
         }
         String title = getFeedBackTitle(mMessage);
         if(TextUtils.isEmpty(mEmail)){
             mEmail = null;
         }

         mTrasactionId = XoneService.getInstance().doFeedBack(XoneConsts.FEEDBACK_TYPE.App,userid, title, mMessage,
         fileId, mEmail,XoneApp.getAppInstance());

    }
    private  String getFeedBackTitle(String content){
//        int len = content.length();
//        if(len<10){
//            return content;
//        }else{
//            return content.substring(0, 10);
//        }   
        return XoneConsts.APP_OS_NAME + XoneUtil.getNumberVersion(getActivity());
    }
    XoneCallBack mXoneCallBack = new XoneCallBack() {

        @Override
        public void onPostLog(int transactionId, String fileId) {
            NTLog.i(TAG, " onPostLog fileId " + fileId);
            doFeedBack(fileId);
        };

        @Override
        public void onPostLogError(int transactionId, int errCode, String err) {
            NTLog.i(TAG, " onPostLogError err");
            doFeedBack(null);
        };

        @Override
        public void onFeedBack(int transactionId) {
            NTLog.i(TAG, "onFeedBack");
//            stopWaiting();
            closeCustomDialog();
            showToast(R.string.feedbackpage_success_tip);
            getActivity().finish();
//            new Handler().postDelayed(new Runnable() {
//
//                @Override
//                public void run() {
//                    getActivity().finish();
//                }
//            }, 1000);
        };
        @Override
        public void onFeedBackError(int transactionId, int errCode, String err) {
            NTLog.i(TAG, "onFeedBackError");
            closeCustomDialog();
//            stopWaiting();
            if(TextUtils.isEmpty(err)){
                showToast(R.string.feedbackpage_err_tip);
            } else {
                showToast(err);
            }
        };
    };
}
